using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Threading;

namespace Sanchay
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            //try
            //{
            Thread t = new Thread(new ThreadStart(SendErrorLogThread));
            t.Start();
            Application.Run(new MDIParent());
            //}
            //catch (Exception ex)
            //{
            //    WriteErrorLog(ex);
            //    return;
            //    //WriteErrorLog(ex);
            //    //MessageBox.Show(ex.Message.ToString() + "\n" + ex.TargetSite.ToString());
            //}
        }

        private static void SendErrorLogThread()
        {
            bool isEmpty = CheckErrorLogIfEmpty();
            if (isEmpty == false)
            {
               // SendErrorLogByEmail();
            }
        }
        private static bool CheckErrorLogIfEmpty()
        {
            StreamReader objStreamReader = new StreamReader("errorlog.txt");
            
            string contentInErrorLog = objStreamReader.ReadToEnd();
            if (contentInErrorLog == "")
                return true;
            else
                return false;
        }
        private static void SendErrorLogByEmail()
        {
            try
            {
                //MailMessage message = new MailMessage();
                //SmtpClient objSmtpClient = new SmtpClient("mail.easymci.org");
                //message.From = new MailAddress("support@easymci.org");
                //message.To.Add("ga7188@gmail.com");
                //message.Subject = "Sanchay-ErrorLog";
                //Attachment objAttachment = new Attachment("errorlog.txt");
                //message.Attachments.Add(objAttachment);
                ////objSmtpClient.Port = 587;
                //objSmtpClient.Credentials = new System.Net.NetworkCredential("support@easymci.org", "easymci123");
                ////objSmtpClient.EnableSsl = true;
                //objSmtpClient.Send(message);
                //message.Attachments.Dispose();
                General objGeneral = new General();
                bool isMailSent = objGeneral.SendEmail("errorlog.txt", "Sanchay-ErrorLog", "");
                if (isMailSent == true)
                {
                    ClearErrorLog();
                }
               
            }
            catch (Exception ex)
            {
                return;
            }
        }
        private static void ClearErrorLog()
        {
            StreamWriter objStreamWriter = new StreamWriter("errorlog.txt");
                objStreamWriter.Write("");
                objStreamWriter.Close();
        }
        private static void WriteErrorLog(Exception ex)
        {
            try
            {
                using (StreamWriter w = File.AppendText("errorlog.txt"))
                {
                    Log(ex.Message.ToString() + " " + ex.TargetSite.ToString(), w);
                    // Close the writer and underlying file.
                    w.Close();
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
        public static void Log(string logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
            // Update the underlying file.
            w.Flush();
        }
    }
}